package com.j3l11234.myy_sign.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.j3l11234.myy_sign.entity.UserEntity;
import com.j3l11234.myy_sign.model.AttendModel;
import com.j3l11234.myy_sign.model.SettingModel;
import com.j3l11234.myy_sign.util.MyException;
import com.j3l11234.myy_sign.util.PrivilegeCheck;



@Controller
@RequestMapping("/manage")
public class ManageController {
	@Autowired
	private SettingModel settingModel;
	@Autowired
	private AttendModel attendModel;

	
	@RequestMapping("/index")
	@PrivilegeCheck(privilege = UserEntity.Manage, needLogin = true)
    public Object index(ModelMap model) {
		model.addAttribute("navbar_item", "manage");
		model.addAttribute("sidebar_item", "index");
		
		//生成近7天的日期
		Date startDate = null, endDate = null;
		Calendar calendar = Calendar.getInstance();
		endDate = calendar.getTime();
		calendar.add(Calendar.DATE, -6);
		startDate = calendar.getTime();
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		//查询签到签退记录
		try {
			Date inTime = settingModel.getSigninTime();
			Date outTime = settingModel.getSignoutTime();
			Map<String,int[]> attendMap = new HashMap<String,int[]>();
			List<String> dateList = new ArrayList<String>();
			for (int i = 0; i < 7; i++) {
				String strDate = dateFormat.format(calendar.getTime());
				attendMap.put(strDate, new int[4]);
				dateList.add(strDate);
				calendar.add(Calendar.DATE, 1);
			}
			int[] total = new int[4];
			attendMap.put("total", total);
			
			List<Map<String, Object>> attendList = attendModel.getAttend(startDate, endDate);
			
			for (Map<String, Object> attend : attendList) {
				int[] stat = attendMap.get(dateFormat.format(attend.get("date")));
				Date curInTime = (Date) attend.get("inTime");
				Date curOutTime = (Date) attend.get("outTime");
				if(curInTime != null){
					if(curInTime.getTime() > inTime.getTime()){
						stat[1]++;
						total[1]++;
					}else{
						stat[0]++;
						total[0]++;
					}
				}
				if(curOutTime != null){
					if(curOutTime.getTime() < outTime.getTime()){
						stat[3]++;
						total[3]++;
					}else{
						stat[2]++;
						total[2]++;
					}
				}					
			}
			
			
			ObjectMapper objectMapper = new ObjectMapper();
			model.addAttribute("attendMap", objectMapper.writeValueAsString(attendMap));
			model.addAttribute("dateList", objectMapper.writeValueAsString(dateList));	
		} catch (MyException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
				
        return "manage/index";
    }
	
	@RequestMapping("/signin")
	@PrivilegeCheck(privilege = UserEntity.Manage, needLogin = true)
    public Object signin(
    		@RequestParam(value="start_date", required = false) String strStartDate,
    		@RequestParam(value="end_date", required = false) String strEndDate,
    		ModelMap model) {
		model.addAttribute("navbar_item", "manage");
		model.addAttribute("sidebar_item", "signin");
		
		//解析开始时间和结束时间
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = null, endDate = null;
        try {
        	startDate = dateFormat.parse(strStartDate);
		} catch (Exception e) {
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, -4);
			startDate = calendar.getTime();
		}
        try {
        	endDate = dateFormat.parse(strEndDate);
		} catch (Exception e) {
			endDate = new Date();
		}
        model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
				
		try {
			List<Map<String, Object>> attendList = attendModel.getAttend(startDate, endDate);
			model.addAttribute("attendList", attendList);
			model.addAttribute("inTime", settingModel.getSigninTime());
		} catch (MyException  e) {
			e.printStackTrace();
		}

        return "manage/signin";
    }
	
	@RequestMapping("/signout")
	@PrivilegeCheck(privilege = UserEntity.Manage, needLogin = true)
    public Object signout(
    		@RequestParam(value="start_date", required = false) String strStartDate,
    		@RequestParam(value="end_date", required = false) String strEndDate,
    		ModelMap model) {
		model.addAttribute("navbar_item", "manage");
		model.addAttribute("sidebar_item", "signout");
		
		//解析开始时间和结束时间
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = null, endDate = null;
        try {
        	startDate = dateFormat.parse(strStartDate);
		} catch (Exception e) {
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, -4);
			startDate = calendar.getTime();
		}
        try {
        	endDate = dateFormat.parse(strEndDate);
		} catch (Exception e) {
			endDate = new Date();
		}
        model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		
		try {
			List<Map<String,Object>> attendList = attendModel.getAttend(startDate, endDate);
			model.addAttribute("attendList", attendList);
			model.addAttribute("outTime", settingModel.getSignoutTime());
		} catch (MyException e) {
			e.printStackTrace();
		}
		
        return "manage/signout";
    }

}
